package cn.edu.stu.syllabus.adapters;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.edu.stu.syllabus.R;
import cn.edu.stu.syllabus.base.BaseQuickClickAdapter;
import cn.edu.stu.syllabus.repository.takeout.local.DishesEntry;

/**
 * Created by daidaijie on 2017/4/13.
 */

public class TakeOutBuyAdapter extends BaseQuickClickAdapter<DishesEntry, TakeOutBuyAdapter.ViewHolder> {

    Map<DishesEntry, Integer> mBuyDishesMap;

    OnNumAddReduceListener mOnNumAddReduceListener;

    public TakeOutBuyAdapter(List<DishesEntry> data, Map<DishesEntry, Integer> buyDishesMap) {
        super(R.layout.item_take_out_buy, data);
        mBuyDishesMap = buyDishesMap;
    }

    @Override
    protected void convert(final ViewHolder helper, final DishesEntry item) {
        helper.mDishesNameTextView.setText(item.getName());
        helper.mDishesPriceTextView.setText(item.getPrice());

        updateItem(helper, item);

        helper.mAddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnNumAddReduceListener != null) {
                    mOnNumAddReduceListener.onAddNum(item, helper.getAdapterPosition());
                    updateItem(helper, item);
                }
            }
        });

        helper.mMinusButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnNumAddReduceListener != null) {
                    mOnNumAddReduceListener.onReduceNum(item, helper.getAdapterPosition());
                    updateItem(helper, item);
                }
            }
        });
    }

    private void updateItem(final ViewHolder helper, final DishesEntry item) {
        if (mBuyDishesMap.containsKey(item) && mBuyDishesMap.get(item) > 0) {
            helper.showCalcEnable(true);
            helper.mBuyNumTextView.setText(mBuyDishesMap.get(item).toString());
        } else {
            helper.showCalcEnable(false);
        }
    }

    public OnNumAddReduceListener getOnNumAddReduceListener() {
        return mOnNumAddReduceListener;
    }

    public void setOnNumAddReduceListener(OnNumAddReduceListener onNumAddReduceListener) {
        mOnNumAddReduceListener = onNumAddReduceListener;
    }

    public interface OnNumAddReduceListener {
        void onAddNum(DishesEntry dishesEntry, int position);

        void onReduceNum(DishesEntry dishesEntry, int position);
    }

    public class ViewHolder extends BaseViewHolder {
        @BindView(R.id.dishesNameTextView)
        TextView mDishesNameTextView;
        @BindView(R.id.dishesPriceTextView)
        TextView mDishesPriceTextView;
        @BindView(R.id.minusButton)
        FrameLayout mMinusButton;
        @BindView(R.id.buyNumTextView)
        TextView mBuyNumTextView;
        @BindView(R.id.addButton)
        FrameLayout mAddButton;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void showCalcEnable(boolean isEnable) {
            if (isEnable) {
                mMinusButton.setVisibility(View.VISIBLE);
                mBuyNumTextView.setVisibility(View.VISIBLE);
            } else {
                mMinusButton.setVisibility(View.INVISIBLE);
                mBuyNumTextView.setVisibility(View.INVISIBLE);
            }
        }
    }
}
